<%@ page language="java" contentType="text/html; charset=utf-8"
	pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<!-- Bootstrap core CSS -->
<link rel="stylesheet" type="text/css"
	href="../../bootstrap-3.3.7/dist/css/bootstrap.min.css" />
<link rel="stylesheet" type="text/css"
	href="../../bootstrap-3.3.7/dist/css/bootstrap-theme.min.css" />
<link rel="stylesheet" type="text/css" href="../../css/index.css" />
<link rel="stylesheet" type="text/css" href="../../css/personal.css" />
<link rel="stylesheet" type="text/css" href="../../css/background.css" />
<script type="text/javascript"
	src="../../bootstrap-3.3.7/dist/js/jquery.min.js"></script>
<script type="text/javascript"
	src="../../bootstrap-3.3.7/dist/js/bootstrap.js"></script>
<script src="../../js/manage/migrate.js"></script>

<!-- bootstrap表格插件 -->
<script src="../../bootstrap-3.3.7/dist/js/bootstrap-table.js"></script>
<link href="../../bootstrap-3.3.7/dist/css/bootstrap-table.css"
	rel="stylesheet" />
<script src="../../bootstrap-3.3.7/dist/js/bootstrap-table-zh-CN.js"></script>
<!-- IE10 viewport hack for Surface/desktop Windows 8 bug -->
<link
	href="../../bootstrap-3.3.7/docs/assets/css/ie10-viewport-bug-workaround.css"
	rel="stylesheet">
<!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries -->
<script src="https://oss.maxcdn.com/html5shiv/3.7.3/html5shiv.min.js"></script>
<script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
</head>
<body>
	<h4 class="heading">知识管理</h4>
	<div class="myline"></div>
	<div class="row" style="margin-top: 10px; margin-left: 10px">
		<div class="form-group">
			<label class="control-label col-sm-2" for="txt_search_departmentname"><span
				style="float: right; margin-top: 5px;">类别</span></label>
			<div class="col-md-9" id="ku" style="float: left"></div>
		</div>
	</div>

	<div class="col-md-12" style="margin-bottom: 0px">
		<form id="formSearch">
			<div class="form-group">
				<label class="control-label col-sm-2"
					for="txt_search_departmentname"><span
					style="float: right; margin-top: 5px">查询</span></label>
				<div class="col-md-2" style="margin-left: 15px">
					<select class="combobox" id="status"
						style="height: 30px; width: 115%;">
						<option value="">全部</option>
						<option value="1">普通</option>
						<option value="2">vip</option>
					</select>
				</div>
				<div class="col-md-5">
					<input type="text" class="form-control" id="infoname"
						placeholder="请输入要搜索的内容...">
				</div>
				<div class="col-md-1" style="text-align: left;">
					<button type="button" style="margin-left: 5px" id="btn_query"
						class="btn btn-primary">查询</button>
				</div>
				<div class="col-md-1" style="text-align: left;">
					<button type="button" style="margin-left: 10px;" id="btn_excel"
						class="btn btn-primary">excel导出</button>
				</div>
			</div>
		</form>
	</div>
	<div class="col-md-12" style="margin-top: 5px; margin-left: 0px">
		<div class="form-group">
			<label class="control-label col-sm-2" for="txt_search_departmentname"><span
				style="float: right; margin-top: 5px">迁移至</span></label>
			<div class="col-md-9" id="mku" style="">
				<button type="button" id="btn_migrate" class="btn btn-primary"
					style="margin-left: 20px">迁移</button>
			</div>
		</div>
	</div>
	<div class="col-md-12" style="margin-top: 5px; margin-left: 85px">
		<div class="col-md-7"></div>
		<div class="col-md-1" style="text-align: left;">
			<button type="button" style="margin-left: 10px;" id="btn_delete"
				class="btn btn-primary">删除</button>
		</div>
		<div class="col-md-1" style="text-align: left;">
			<button type="button" style="margin-left: 10px;" id="btn_tagvip"
				class="btn btn-primary">标记vip</button>
		</div>
		<div class="col-md-1" style="text-align: left;">
			<button type="button" style="margin-left: 20px;" id="btn_cancelvip"
				class="btn btn-primary">取消vip</button>
		</div>
	</div>

	<div class="col-md-12">
		<table id="tb_departments">
		</table>
	</div>
</body>
<script type="text/javascript">
	var count = 0;
	//html_id为HTML页面上要添加位置的ID
	function KuBindNew(html, html_id, level, pId) {
		//清空下拉数据		
		for (var x = level; x <= count; x++) {
			$("#" + html + x).remove();
		}	
		var str = "";
		$
				.ajax({
					type : "POST",
					url : "../configration/getReczsk",
					data : {
						"id" : "",
						"pId" : pId,
						"name" : "",
						"value" : "",
						"level" : level
					},
					dataType : "JSON",
					async : false,
					success : function(data) {
						if (data.length != 0) {
							str += '<div id='+html+level+' class="col-md-3"  style="margin-bottom: 15px"><select id='
									+ html_id
									+ level
									+ ' name="kulist" style="height: 30px; width: 100%; float: left">';
							//从服务器获取数据进行绑定
							str += "<option value='all'>全部</option>";
							$.each(data, function(i, item) {
								str += "<option value=" + item.id + ">"
										+ item.name + "</option>";
							})
							str += "</select></div>";
							//将数据添加到省份这个下拉框里面
							$("#" + html_id).append(str);
							count++;
						}
					},
					error : function() {
						alert("Error");
					}
				});
	}
	function dynamicBindNew(html, html_id, level) {
		for (var i = level; i <= count; i++) {
			(function(m) {
				$("#" + html_id + m).change(
						function() {
								KuBindNew(html, html_id, m + 1,$("#" + html_id + m).val());
								dynamicBindNew(html, html_id, m + 1);						
						});
			})(i);
		}
	}
	$(function() {
		 //初始化动态库标签内容
		KuBindNew("kudiv", "ku", 1, "");
		dynamicBindNew("kudiv", "ku", 1);
		//标签迁移
		mKuBindNew("mkudiv", "mku", 1, "");
		mdynamicBindNew("mkudiv", "mku", 1);

		//1.初始化Table
		var oTable = new TableInit();
		oTable.Init();

		//2.初始化Button的点击事件
		var oButtonInit = new ButtonInit();
		oButtonInit.Init();

	});
	var TableInit = function() {
		var oTableInit = new Object();
		//初始化Table
		oTableInit.Init = function() {
			$('#tb_departments')
					.bootstrapTable(
							{
								url : '../information/getInfomanage', //请求后台的URL（*）
								method : 'get', //请求方式（*）
								toolbar : '#toolbar', //工具按钮用哪个容器
								striped : true, //是否显示行间隔色
								cache : false, //是否使用缓存，默认为true，所以一般情况下需要设置一下这个属性（*）
								pagination : true, //是否显示分页（*）
								sortable : true, //是否启用排序
								sortOrder : "asc", //排序方式
								queryParams : oTableInit.queryParams,//传递参数（*）
								sidePagination : "server", //分页方式：client客户端分页，server服务端分页（*）
								pageNumber : 1, //初始化加载第一页，默认第一页
								pageSize : 10, //每页的记录行数（*）
								pageList : [10, 25, 50, 100], //可供选择的每页的行数（*）
								search : false, //是否显示表格搜索，此搜索是客户端搜索，不会进服务端，所以，个人感觉意义不大
								strictSearch : true,
								showColumns : true, //是否显示所有的列
								showRefresh : true, //是否显示刷新按钮
								minimumCountColumns : 2, //最少允许的列数
								clickToSelect : true, //是否启用点击选中行
								height : 650, //行高，如果没有设置height属性，表格自动根据记录条数觉得表格高度
								uniqueId : "info_id", //每一行的唯一标识，一般为主键列
								showToggle : true, //是否显示详细视图和列表视图的切换按钮
								cardView : false, //是否显示详细视图
								detailView : false, //是否显示父子表
								columns : [
									{
										checkbox : true
									},
									{
										field : 'info_name',
										title : '题名',
										width : '30%',
										formatter : function(value,row,index) {
											var element = "<a onclick='check(\""+row.info_id+"\")' data-id='"+ row.info_id+ "'><span class='text-primary'>"+ row.info_name+ "</span></a>&nbsp;&nbsp";

											return element;
										}
									}, 
									{
										field : 'info_author',
										title : '作者',
										width : '13%'
									},
									{
										field : 'info_bm',
										title : '发布部门',
										width : '12%'
									},
									{
										field : 'info_time',
										title : '发布时间',
										width : '12%'
									},
									{
										field : 'library',
										title : '库',
										width : '10%'
									},
									{
										field : 'info_download',
										title : '下载',
										width : '5%',
									},
									{
										field : 'info_browser',
										title : '浏览',
										width : '5%',
									},
										{
											field : 'operate',
											title : '操作',
											width : '15%',
											formatter : function(value, row,
													index) {
												var element='<a  data-id="\''+ row.info_id+ '\'" onclick="deleteone(\''+ row.info_id+ '\',\''+row.info_name+'\',\''+row.info_url+'\',\''+row.first_class+'\')"><span class=\'text-primary\'>删除</span></a>&nbsp;&nbsp;'
													+'<a href="#" data-id="\''+ row.info_id+ '\'" onclick="updateInfo(\''+row.info_id+ '\')"><span class=\'text-primary\'>修改</span></a><br>'
												if(row.info_level=='1'){
													element+='<a data-id="\''+ row.info_id+ '\'" class=\'text-primary\' id="vipset" onclick="updateInfolevel(\''+row.info_id+ '\',\'2\')">标记vip</a>';
												}
												if(row.info_level=='2'){
													element+='<a data-id="\''+ row.info_id+ '\'" class=\'text-primary\' id="vipset" onclick="updateInfolevel(\''+row.info_id+ '\',\'1\')">取消vip</a>';
												}
												return element;
											}
										}]
							});
		};
		//得到查询的参数
		oTableInit.queryParams = function(params) {
		
			var kudata=null;
			var kulist = $("select[name='kulist']");
				kudata = kulist[kulist.length-1].value;
			
			if(kulist.length>=2){
				if(kudata=='all')
					 kudata = kulist[kulist.length-2].value;
			}
			if(kudata=='all') kudata =null; 
			var status = $("#status").val();
			var temp = { //这里的键的名字和控制器的变量名必须一直，这边改动，控制器也需要改成一样的
				limit : params.limit, //页面大小
				offset : params.offset, //页码初始开始值
				infoname : $("#infoname").val(),
				kudata:kudata,
				level:$("#status").val()
			};
			return temp;
		};
		return oTableInit;
	};

	var ButtonInit = function() {
		var oInit = new Object();
		oInit.Init = function() {
			//初始化页面上面的按钮事件
			$('#btn_query').bind('click', function() {
				$('#tb_departments').bootstrapTable("refresh", {
				});
			});
			
			$('#btn_excel').bind('click',function(){
				var kudata=null;
				var kulist = $("select[name='kulist']");
					kudata = kulist[kulist.length-1].value;
				
				if(kulist.length>=2){
					if(kudata=='all')
						 kudata = kulist[kulist.length-2].value;
				}
				if(kudata=='all') kudata =null;
				var infoname = $("#infoname").val();
				var infolevel = $("#status").val();
				var url;
				if(kudata==null)
					url="../information/getExcelFile?infoname="+infoname+"&level="+infolevel;
				else
					url="../information/getExcelFile?infoname="+infoname+"&kudata="+kudata+"&level="+infolevel
				window.location.href =encodeURI(url);
			});
			
			$('#btn_delete').bind(
					'click',
					function() {
					 	var deleteRows = $('#tb_departments').bootstrapTable("getSelections");
						if(deleteRows.length==0){
							alert("请选择要删除的数据");
							return;
						}
						var strIds=[];
						var strurls=[];
						var strnames=[];
						var strkutypes=[];
						for(var i=0;i<deleteRows.length;i++){
							strIds.push(deleteRows[i].info_id);
							strurls.push(deleteRows[i].info_url);
							strnames.push(deleteRows[i].info_name);
							strkutypes.push(deleteRows[i].first_class);
						}
						var ids=strIds.join(",");
						if (confirm("您确定删除这"+deleteRows.length+"条数据吗?")) {
							$.ajax({
								type : 'POST',
								url : '../information/deleteUploads',
								data : {
							        ids :ids,urls:strurls,names:strnames,kutypes:strkutypes,zt:"1"
								},
								traditional: true,
								success : function(result) {
									if(result>0){
										alert("删除成功");
										$('#tb_departments').bootstrapTable("refresh", {
										});
									}
									$('#tb_departments').bootstrapTable("refresh", {
									});
								}
							});
						}	
					 });
			$('#btn_tagvip').bind(
					'click',
					function() {
						var updateRows = $('#tb_departments').bootstrapTable("getSelections");
						if(updateRows.length==0){
							alert("请选择知识");
							return;
						}
						var strIds=[];
						for(var i=0;i<updateRows.length;i++){
							strIds.push(updateRows[i].info_id);
						}
						var ids=strIds.join(",");
						if (confirm("您确定标记这"+updateRows.length+"条为vip数据吗")) {
							$.ajax({
								type : 'POST',
								url : '../information/updateInfoslevel',
								data : {
							        ids :ids,level:'2'
								},
								traditional: true,
								success : function(result) {
									if(result>0){
										alert("操作成功");
										$('#tb_departments').bootstrapTable("refresh", {
										});
									}
								}
							});
						}	
					});
			$('#btn_cancelvip').bind(
					'click',
					function() {
						var updateRows = $('#tb_departments').bootstrapTable("getSelections");
						if(updateRows.length==0){
							alert("请选择知识");
							return;
						}
						var strIds=[];
						for(var i=0;i<updateRows.length;i++){
							strIds.push(updateRows[i].info_id);
						}
						var ids=strIds.join(",");
						if (confirm("您确定取消"+updateRows.length+"条数据vip标记吗")) {
							$.ajax({
								type : 'POST',
								url : '../information/updateInfoslevel',
								data : {
							        ids :ids,level:'1'
								},
								traditional: true,
								success : function(result) {
									alert(result);
									if(result>0){
										alert("操作成功");
										$('#tb_departments').bootstrapTable("refresh", {
										});
									}
									
								}
							});
						}	
					});
			$('#btn_migrate').bind(
					'click',
					function() {
						var kudata=null;
						var kulist = $("select[name='mkulist']");
							kudata = kulist[kulist.length-1].value;
						if(kudata=='all') {
							alert("请选择所属的库类别");
							return;
						} 
						var updateRows = $('#tb_departments').bootstrapTable("getSelections");
						if(updateRows.length==0){
							alert("请选择知识");
							return;
						}
						var strIds=[];
						for(var i=0;i<updateRows.length;i++){
							strIds.push(updateRows[i].info_id);
						}
						var ids=strIds.join(",");
						if (confirm("您确定迁移这"+updateRows.length+"条数据吗?")) {
							$.ajax({
								type : 'POST',
								url : '../information/updateInfosclass',
								data : {
							        ids :ids,kudata:kudata
								},
								traditional: true,
								success : function(result) {
									if(result>0){
										alert("迁移成功");
										$('#tb_departments').bootstrapTable("refresh", {
										});
									}
								}
							});
						}	
					});
		};
		return oInit;
	};
	function deleteone(id,name,infourl,kutype){
		if (confirm("您确定删除这条数据吗?")) {
			$.ajax({
				type : 'POST',
				url : '../information/deleteUpload',
				data : {
			        id :id,
			        name:name,
			        infourl:infourl,
			        kutype:kutype,
			        zt:"1"
				},
				success : function(result) {
					if(result>0){
						alert("删除成功");
						$('#tb_departments').bootstrapTable("refresh", {
						});
					}
				}
			});
		}	
	};
	//查看知识详情
	function check(id) {
		window.open("../information/getInfoDetial?info_id="+id);
	}
	function updateInfo(index) {
		var row = $('#tb_departments').bootstrapTable('getRowByUniqueId', index);
		  var info={
					"info_id":row.info_id,
					"info_name":row.info_name,
					"info_author":row.info_author,
					"info_company":row.info_company,
					"info_keyword":row.info_keyword,
					"info_time":row.info_time,
					"info_bm":row.info_bm,
					"info_source":row.info_source,
					"info_wh":row.info_wh,
					"info_abstract":row.info_abstract,
					"info_url":row.info_url,
					"first_class":row.first_class
			   };
		$("#refresh").load("../information/getUpdatemview?info="+encodeURI(JSON.stringify(eval(info))));
	}
	
	function updateInfolevel(infoid,level){
		$.ajax({
			type:'post',
			url:'../information/updateInfolevel',
			data:{
				level:level,
				infoid:infoid
			},
			success:function(result){
				if(result==1){
					alert("设置成功");
					$('#tb_departments').bootstrapTable("refresh", {
					});
				}
			}
		});
	}
</script>
</html>